package com.gengealogy.net.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gengealogy.net.po.Clan;
import com.gengealogy.net.po.Member;
import com.gengealogy.net.service.ClanService;
import com.gengealogy.net.service.MemberService;

@Controller
public class ClanController {

	@Autowired
	private ClanService clanService;
	@Autowired
	private MemberService memberService;

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public ClanService getClanService() {
		return clanService;
	}

	public void setClanService(ClanService clanService) {
		this.clanService = clanService;
	}

	@RequestMapping(value = "/build/getClan.action")
	public String getClan(Model model, HttpSession session) {
		
		int clan_create_id = (int) session.getAttribute("USER_ID");
		Clan clan=clanService.findClan(clan_create_id);
		if(clan==null) {
			model.addAttribute("msg", "还未创建家族，请先创建！");
			return "build/gen_create";
		}else {
			int tree_clan_id = clan.getClan_id();
			session.setAttribute("TREE_CLAN_ID", tree_clan_id);
			model.addAttribute("clan", clan);
			return "build/gen_entry";
			
		}
	}
	
	
	@RequestMapping(value = "/user/userAddClan.action")
	public String userAddClan(Model model, HttpSession session) {
		
		int member_user_id = (int) session.getAttribute("USER_ID");
		Member member=memberService.findUserMember(member_user_id);
		if(member==null) {
			model.addAttribute("msg", "还未加入家族，请先加入！");
			return "user/gen_create";
		}else {
			Clan clan=clanService.getClanById(member.getMember_clan_id());
			int tree_clan_id =clan.getClan_id();
			session.setAttribute("TREE_CLAN_ID", tree_clan_id);
			model.addAttribute("clan", clan);
			return "user/gen_entry";
			
		}
	}
	
	/**
	 * 向创建家族页面跳转
	 */
	@RequestMapping(value = "/addClan.action", method = RequestMethod.GET)
	public String toAddClan() {
	    return "build/gen_entry";
	}
	
	@RequestMapping(value = "/addClan.action", method = RequestMethod.POST)
	public String addClan(Clan clan,Model model){
		clanService.addClan(clan);
		return "redirect:build/getClan.action";
	}

	@RequestMapping(value = "/clan/updateClan.action", method = RequestMethod.POST)
	@ResponseBody
	public String updateClan(Clan clan, Model model) {
		int rows =clanService.updateClan(clan);
		if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	
	@RequestMapping("/getClanById.action")
	@ResponseBody
	public Clan getClanById(Model model, HttpSession session) {
		int clan_id = (int) session.getAttribute("TREE_CLAN_ID");
		Clan clan = clanService.getClanById(clan_id);
	    return clan;
	}
}
